<?php
use think\migration\Migrator;
use think\migration\db\Column;

class CreateApprovalTables extends Migrator
{
    public function change()
    {
        // 审批流程主表
        $table = $this->table('approval_flow', ['comment' => '审批流程主表']);
        $table->addColumn('title', 'string', ['limit' => 255, 'comment' => '流程标题'])
            ->addColumn('creator_id', 'integer', ['comment' => '创建人ID'])
            ->addColumn('status', 'integer', ['limit' => 1, 'default' => 0, 'comment' => '状态:0待审批,1已通过,2已拒绝,3已取消'])
            ->addColumn('created_at', 'integer', ['comment' => '创建时间'])
            ->addColumn('updated_at', 'integer', ['comment' => '更新时间'])
            ->create();

        // 审批节点表
        $table = $this->table('approval_nodes', ['comment' => '审批节点表']);
        $table->addColumn('flow_id', 'integer', ['comment' => '关联流程ID'])
            ->addColumn('name', 'string', ['limit' => 100, 'comment' => '节点名称'])
            ->addColumn('type', 'integer', ['limit' => 1, 'comment' => '类型:1标签,2个人'])
            ->addColumn('condition', 'string', ['limit' => 500, 'null' => true, 'comment' => '条件表达式'])
            ->addColumn('sort', 'integer', ['default' => 0, 'comment' => '排序'])
            ->addIndex(['flow_id'])
            ->create();

        // 审批人表
        $table = $this->table('approval_approvers', ['comment' => '审批人表']);
        $table->addColumn('node_id', 'integer', ['comment' => '关联节点ID'])
            ->addColumn('approver_type', 'integer', ['limit' => 1, 'comment' => '审批人类型:1标签,2个人'])
            ->addColumn('approver_id', 'string', ['limit' => 100, 'comment' => '审批人ID或标签'])
            ->addIndex(['node_id'])
            ->create();

        // 审批记录表
        $table = $this->table('approval_records', ['comment' => '审批记录表']);
        $table->addColumn('flow_id', 'integer', ['comment' => '关联流程ID'])
            ->addColumn('node_id', 'integer', ['comment' => '关联节点ID'])
            ->addColumn('approver_id', 'integer', ['comment' => '审批人ID'])
            ->addColumn('status', 'integer', ['limit' => 1, 'comment' => '状态:1通过,2拒绝'])
            ->addColumn('comment', 'string', ['limit' => 500, 'null' => true, 'comment' => '审批意见'])
            ->addColumn('created_at', 'integer', ['comment' => '创建时间'])
            ->addIndex(['flow_id'])
            ->addIndex(['node_id'])
            ->create();

        // 抄送表
        $table = $this->table('approval_cc', ['comment' => '抄送表']);
        $table->addColumn('flow_id', 'integer', ['comment' => '关联流程ID'])
            ->addColumn('cc_type', 'integer', ['limit' => 1, 'comment' => '抄送类型:1标签,2个人'])
            ->addColumn('cc_id', 'string', ['limit' => 100, 'comment' => '抄送人ID或标签'])
            ->addColumn('node_id', 'integer', ['null' => true, 'comment' => '关联节点ID'])
            ->addIndex(['flow_id'])
            ->create();
    }
}
